﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace WinFormsApp_hw0407_new
{
    public class IconIndexes
    {
        public const int MyComputer = 0;      //我的电脑
        public const int ClosedFolder = 1;    //文件夹关闭
        public const int OpenFolder = 2;      //文件夹打开
        public const int FixedDrive = 3;      //磁盘盘符
        public const int MyDocuments = 4;     //我的文档
    }

    public static class TreeViewItems
    {
        /*public static void Add(TreeNode e)
        {
            try
            {
                //判断"我的电脑"Tag 上面加载的该结点没指定其路径
                if (e.Tag.ToString() != "我的电脑")
                {
                    e.Nodes.Clear();                               //清除空节点再加载子节点
                    TreeNode tNode = e;                            //获取选中\展开\折叠结点
                    string path = tNode.Name;                      //路径  

                     //获取"我的文档"路径
                    if (e.Tag.ToString() == "我的文档")
                    {
                        path = Environment.GetFolderPath           //获取计算机我的文档文件夹
                            (Environment.SpecialFolder.MyDocuments);
                    } 

                    //获取指定目录中的子目录名称并加载结点
                    string[] dics = Directory.GetDirectories(path);
                    foreach (string dic in dics)
                    {
                        TreeNode subNode = new TreeNode(new DirectoryInfo(dic).Name); //实例化
                        subNode.Name = new DirectoryInfo(dic).FullName;               //完整目录
                        subNode.Tag = subNode.Name;
                        subNode.ImageIndex = IconIndexes.ClosedFolder;       //获取节点显示图片
                        subNode.SelectedImageIndex = IconIndexes.OpenFolder; //选择节点显示图片
                        tNode.Nodes.Add(subNode);
                        subNode.Nodes.Add("");                               //加载空节点 实现+号
                    }
                }
                else
                    Add(e.NextNode);
            }
            catch (Exception msg)
            {
                MessageBox.Show(msg.Message);                   //异常处理
            }
        }*/
        public static void Add(object sender, TreeViewCancelEventArgs e)
        {
            e.Node.Nodes.Clear();
            TreeNode tNode = e.Node;
            //MessageBox.Show(tNode.Name);
            string path = tNode.Name;
            string[] dics = Directory.GetDirectories(path);
            foreach (string dic in dics)
            {
                TreeNode subNode = new TreeNode(new DirectoryInfo(dic).Name);
                subNode.Name = new DirectoryInfo(dic).FullName;
                subNode.Tag = subNode.Name;
                subNode.Nodes.Add("");
                tNode.Nodes.Add(subNode);

            }
            string[] files = Directory.GetFiles(path);
            foreach (string aFile in files)
            {
                TreeNode subNode = new TreeNode(new FileInfo(aFile).Name);
                subNode.Tag = aFile;
                subNode.Name = subNode.Text;
                tNode.Nodes.Add(subNode);
            }
        }
    }

}
